Page({
  data: {
    skills: [],
    newSkill: ''
  },

  onLoad() {
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
	if(prevPage !== undefined) {
		this.setData({ skills: prevPage.data.skills })
	}
  },

  inputSkill(e) {
    this.setData({ newSkill: e.detail.value })
  },

  addSkill() {
    if (!this.data.newSkill.trim()) return
    
    this.setData({
      skills: [...this.data.skills, this.data.newSkill],
      newSkill: ''
    })
  },

  removeSkill(e) {
    const index = e.currentTarget.dataset.index
    const newSkills = this.data.skills.filter((_, i) => i !== index)
    this.setData({ skills: newSkills })
  },

  saveSkills() {
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    prevPage.setData({ skills: this.data.skills })
    wx.navigateBack()
  }
})